package com.pic.mgt.monitor.parser.syushin;

import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.monitor.parser.FormatParser;

public final class SimpleTerminalIDParser extends FormatParser {
	private int terminalIDFromChar;
	private int terminalIDEndToChar;

	@Override
	public String parse(Object _confirmationFilename)
							throws WrongUnitFormatException {
		// 確認是否為 String 型態
		// throws WrongUnitFormatException
		super.checkObjectType(_confirmationFilename, String.class);
		
		String confirmationFilename = (String)_confirmationFilename;
		
		String terminalID = null;
		try {
			// throws StringIndexOutOfBoundsException
			terminalID = confirmationFilename.
							substring(this.terminalIDFromChar - 1,
									  this.terminalIDEndToChar);
		} catch (StringIndexOutOfBoundsException e) {			
			throw new WrongUnitFormatException(
					"Confirmation filename format is invalid.", e);
		}
		
		return terminalID;
	}

	// --- getter and setter ---
	
	public void setTerminalIDFromChar(int terminalIDFromChar) {
		this.terminalIDFromChar = terminalIDFromChar;
	}

	public void setTerminalIDEndToChar(int terminalIDEndToChar) {
		this.terminalIDEndToChar = terminalIDEndToChar;
	}
}
